package com.we.web.servlet;

import com.we.entity.*;
import com.we.service.TagService;
import com.we.service.impl.TagServiceImpl;
import com.we.util.Base64Utils;
import com.we.util.JsonUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 标签请求处理
 * @Author cr7
 * @Date 2021-11-14 19:42
 * @Version V1.0
 */
@WebServlet("/tag")
public class TagServlet extends BaseServlet {

    private TagService service = new TagServiceImpl();
    private ResultInfo info = new ResultInfo();

    //查询所有标签数量
    public void findTagCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int count = service.findTagCount();
        if (count < 0) {
            info.setFlag(false);
            info.setData("查询标签数量失败");
        } else {
            info.setData(true);
            info.setData(count);
        }
        JsonUtils.writeValue(info, resp);
    }

    //查询所有标签及其数量方法
    public void findAllTag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List list = service.findAllTag();
        if (list.isEmpty()) {
            info.setFlag(false);
            info.setErrorMsg("当前无标签信息");
        } else {
            info.setFlag(true);
            info.setData(list);
        }
        JsonUtils.writeValue(info, resp);
    }

    //查询所由标签及其数量方法(分页)
    public void findAllTagByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPageStr = req.getParameter("currentPage");
        String pageSizeStr = req.getParameter("pageSize");

        int currentPage = 0;//当前页码，如果不传递，则默认为第一页
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }

        int pageSize = 0;//每页显示条数，如果不传递，默认每页显示5条记录
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 6;
        }

        PageBean<Tag> pb = service.findAllTagByPage(currentPage, pageSize);
        JsonUtils.writeValue(pb, resp);
    }

    //添加标签方法
    public void addTag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String tagName = req.getParameter("tagName");
        if (StringUtils.isBlank(tagName)) {
            info.setFlag(false);
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.addTag(tagName);
        if (count > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("添加失败");
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据标签名查询标签方法
    public void findTagByTagName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tagName = req.getParameter("tagName");
        if (StringUtils.isBlank(tagName)) {
            info.setFlag(false);
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.findTagByTagName(tagName);
        if (count > 0) {
            info.setFlag(false);
            info.setErrorMsg("标签名重复，请检查");
        } else {
            info.setFlag(true);
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据标签名删除标签
    public void deleteTagByTagName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String tagName = req.getParameter("tagName");
        if (StringUtils.isBlank(tagName)) {
            info.setFlag(false);
            info.setErrorMsg("分类名不能为空");
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.deleteTagByTagName(tagName);
        if (count > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("删除失败");
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据标签名修改标签方法
    public void updateTagName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (!user.getIsAuthor().equals("root") || user == null) {
            info.setFlag(false);
            info.setErrorMsg("您没有相应的权限");
            JsonUtils.writeValue(info, resp);
            return;
        }

        String tagName = req.getParameter("tagName");
        String tagName1 = req.getParameter("tagName1");
        if (StringUtils.isBlank(tagName) || StringUtils.isBlank(tagName1)) {
            info.setFlag(false);
            info.setErrorMsg("标签名不能为空");
            JsonUtils.writeValue(info, resp);
            return;
        }

        int count = service.updateTagName(tagName, tagName1);
        if (count < 0) {
            info.setFlag(false);
            info.setErrorMsg("修改失败");
        } else {
            info.setFlag(true);
        }

        JsonUtils.writeValue(info, resp);
    }

    //根据标签下的笔记方法
    public void findNoteByTag(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String tagName = (req.getParameter("tagName"));
        if (StringUtils.isBlank(tagName.trim())) {
            resp.getWriter().write("<script>alert(标签查询失败)</script>");
        } else {
            String currentPageStr = req.getParameter("currentPage");
            String pageSizeStr = req.getParameter("pageSize");

            int currentPage = 0;//当前页码，如果不传递，则默认为第一页
            if (currentPageStr != null && currentPageStr.length() > 0) {
                currentPage = Integer.parseInt(currentPageStr);
            } else {
                currentPage = 1;
            }

            int pageSize = 0;//每页显示条数，如果不传递，默认每页显示5条记录
            if (pageSizeStr != null && pageSizeStr.length() > 0) {
                pageSize = Integer.parseInt(pageSizeStr);
            } else {
                pageSize = 5;
            }

            PageBean<Note> pb = service.findNoteByTag(tagName, currentPage, pageSize);
            JsonUtils.writeValue(pb, resp);
        }
    }

}
